/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-07 16:13:34
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-03 10:54:34
 * @FilePath: \cxl-driving-training\src\utils\dict-data.ts
 */

import { dictInformationByDictId, dictList, dictOperateProps, getAllDict } from '@/apis/system/dict'
import { storage } from '@/store/sessionStorage'

export interface optionsInterface {
    label: string
    value: string
    type?: string
}

export interface DictInterface {
    label: string
    value: string
    color?: string
}

interface dynamicProp {
    [name: string]: string
}

//颜色类型
export const colorType: { value: string }[] = [
    { value: '#409EFF' },
    { value: '#67C23A' },
    { value: '#E6A23C' },
    { value: '#F56C6C' },
    { value: '#000000' },
    { value: '#303133' },
    { value: '#606266' },
    { value: '#909399' },
    { value: '#A8ABB2' },
    { value: '#C0C4CC' }
]

//map数据转数组
const mapToObjectArray = (map: Map<any, any>) => Array.from(map.entries(), ([key, value]) => [key, value])

class DICT {
    LIST = new Map<string, any>() //保存所有的 【key value】
    constructor() {
        // this.getDictData()
    }
    //拉去所有的字典key
    getDictData(clear = false) {
        if (clear) {
            storage.remove('DICT_DATA')
        }
        const dictStr = storage.get('DICT_DATA')
        if (dictStr && dictStr !== 'null') {
            const data = JSON.parse(dictStr)
            for (const key in data) {
                const valList = JSON.parse(data[key])
                // console.log(key, valList)
                const arr: DictInterface[] = []
                valList.forEach((item: any) => {
                    arr.push({
                        label: item.text,
                        value: item.value,
                        color: item.color || ''
                    })
                })

                this.LIST.set(key, arr)
            }
            // console.log(this.LIST)
            return
        }
        getAllDict().then(res => {
            for (const key in res.data.result) {
                const valList = JSON.parse(res.data.result[key])
                const arr: DictInterface[] = []
                valList.forEach((item: any) => {
                    arr.push({
                        label: item.text,
                        value: item.value,
                        color: item.color || ''
                    })
                })

                this.LIST.set(key, arr)
            }
            storage.set('DICT_DATA', JSON.stringify(res.data.result))
        })
    }
    has(key: string): boolean {
        return this.LIST.has(key)
    }
    get(key: string) {
        return this.LIST.get(key)
    }
}

export const DICT_DATA = new DICT()
